const router = require("koa-router")();
const { generateToken } = require("../../config/jwt");
const ico = require("../ico");
router.prefix("/api");
const { _Mysql } = require('../../appConfig')

router.post("/login", async function (ctx, next) {
  if(_Mysql.look){
      let body = ctx.request.body;
      if (body.account && body.password) {
          const result =
              (await ico["user"].findAll({
                  where: { account: body.account, password: body.password },
              })) || [];
          if (result.length !== 0) {
              return ctx.body = {
                  account: body.account,
                  token: generateToken(result),
                  failure_time: Date.parse(new Date()) / 1000 + 24 * 60 * 60
              }
          }
      }
  }else{
      return ctx.body = {
          account: '请配置数据库',
          token: generateToken(),
          failure_time: Date.parse(new Date()) / 1000 + 24 * 60 * 60
      }
  }
  throw new Error("账号或密码错误");
});
router.get("/loginInfo", function (ctx, next) {
  ctx.body = ctx.token_data;
});

module.exports = router;
